<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet"
        href="https://cdn.cnbj1.fds.api.mi-img.com/mi.com-assets/shop/pro/css/buy/cart2.382798e6.css">
    <link rel="stylesheet" href="../css/reset.css">
    <script src="../js/vue.js"></script>
    <style>
        /* span的默认样式 */
        .lw-checkbox {
            user-select: none;
            display: inline-block;
        }

        .lw-checkbox label span {
            display: inline-block;
            vertical-align: middle;
            width: 20px;
            height: 20px;
            border-radius: 2px;
            border: 1px solid #757575;
            color: #fff;
            text-align: center;
            line-height: 20px;

        }

        .lw-checkbox label span:hover {
            color: #757575;
        }

        /* 当checkbox处于选中状态 span的样式 */
        .lw-checkbox label input[type='checkbox']:checked+span {
            background-color: #ff6700;
            border-color: #ff6700;
            color: #fff;
        }

        .content {
            font-style: normal;
        }
    </style>
</head>

<body>
    <div id="app">
        <!-- <div class="lw-checkbox">
            <label>
                <input hidden type="checkbox" v-model="checked">
                <span class="iconfont">√</span>
            </label>
        </div> -->
        <!-- <p>checked:{{checked}}</p> -->

        <!-- .sync 修饰符  => 常用于子父组件通信, 添加此修饰符之后 会自动给子组件绑定自定义事件
            使用时: :参数.sync='变量'
            默认绑定自定义事件: @update:参数="(v)=> 变量 = v"

            缺点: Vue2  , Vue3已经废弃

        -->
        <!-- <lw-checkbox :checked="flag" @update:checked="(v)=>flag=v" label="是否记住用户名?"></lw-checkbox> -->
        <lw-checkbox :checked.sync="flag" label="是否记住用户名?"></lw-checkbox>
        <p>
            flag:{{flag}}
        </p>
    </div>
</body>
<script>

    Vue.component("lw-checkbox", {
        props: {
            checked: {
                type: Boolean,
                default: false,
            },
            label: {
                type: String,
                default: "",
            }
        },
        data() {
            return {

            }
        },

        // v-model="checked" 双向数据绑定 
        // 1. 数据改变,更新视图
        // 2. 视图触发事件,数据也会更新  => 将更新后的值赋值给checked (违背了单向数据流的原则)
        // template: `<div class="lw-checkbox">
        //     <label>
        //         <input hidden type="checkbox" v-model="checked" @change="$emit('update:checked',checked)">
        //         <span class="iconfont">√</span>
        //         <i class="content">{{label}}</i>
        //     </label>
        // </div>`,
        template: `<div class="lw-checkbox">
            <label>
                <input hidden type="checkbox" :checked="checked" @change="$emit('update:checked',$event.target.checked)">
                <span class="iconfont">√</span>
                <i class="content">{{label}}</i>
            </label>
        </div>`
    })


    var app = new Vue({
        el: "#app",
        data: {
            flag: false,
        },
        methods: {
            updateCheckedHandler(v) {
                this.flag = v;
            }
        },
    })

</script>

</html>